﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.WeiXin;
using Xproject.Framework.Utility.Extensions;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.ExceptionHandling;
using Xproject.Framework.Core;
using System.Linq.Expressions;
using Model.WeiXin.DTO;
using Model.WXshopBuiness;

namespace Repository.WeiXinProject
{
    public static class WeiXinUserInfoRep
    {
       
        public static WeiXinUser Get(this IRepositoryAsync<WeiXinUser> repository, string id)
        {
            Expression<Func<WeiXinUser, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.UserID == id);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }
        public static WeiXinUser GetByOpenId(this IRepositoryAsync<WeiXinUser> repository, string id)
        {
            Expression<Func<WeiXinUser, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.OpenId == id);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }

        public static ZXUserInfoModel GetZXUserInfoByOpenId(this IRepositoryAsync<WeiXinUser> repository, string openId)
        {
            string exeSql = string.Format(@"SELECT  [UserID],[GradeID],[UserCode],[Account],[Password],[OpenId],[NickName],[WXAccount]
            ,[Area],[HeadUrl],[ParentID],[IDPath],WU.[CreateTime],[State],[ProjectID],[Email],[Sex],[BirthDay],[IsAutoSynchro],[TJCode]
            ,[RegisterSource],[TotalSaleMoney],[TotalBonusMoney],[BonusBalance],[TotalRequestMoney],[ScoreBalance],[FrozenBalance],[FrozenScore],[UnionId]
            ,[ExpireTime],[CompanyName],[UserName],[IDCard],[Mobile],[PayPwd],[IsFocus],[FocusTime],[CancelTime],[IsTrade],[IsRealAuth]
            ,[IsGoldAuth],[IsCompanyAuth],[IsAuth],[RedOpenId],[PayOpenId],[AuthOpenId],[MsgOpenId],[LastLoginTime],[IsMobileAuth], 
            ZXU.ZX_UserId ,ZXU.ZX_UserName,ZXU.ZX_UserPwd,ZXU.ZX_UserLevel,ZXU.ZX_UserParentId,ZXU.ZX_UserSource,ZXU.ZX_Platform,ZXU.ZX_UserType,ZXU.JiFeng,
            ZXU.[BindPhone],ZXU.[BindQQ],ZXU.[BindWeiXin],ZXU.[BindZhiFuBao],ZXU.[BindBankCard],ZXU.[HaveEarnMoney]
              FROM [WeiXin_User] WU
              left join ZX_UserInfo ZXU ON 
              WU.UserID = ZXU.WX_UserId
  WHERE WU.OpenId = '{0}'", openId);
           return repository.SqlQueryT<ZXUserInfoModel>(exeSql).FirstOrDefault();
        }

        public static ZXUserInfoModel GetZXUserInfoByUserCode(this IRepositoryAsync<WeiXinUser> repository, string userCode, string password)
        {
            string exeSql = string.Format(@"SELECT  [UserID],[GradeID],[UserCode],[Account],[Password],[OpenId],[NickName],[WXAccount]
            ,[Area],[HeadUrl],[ParentID],[IDPath],WU.[CreateTime],[State],[ProjectID],[Email],[Sex],[BirthDay],[IsAutoSynchro],[TJCode]
            ,[RegisterSource],[TotalSaleMoney],[TotalBonusMoney],[BonusBalance],[TotalRequestMoney],[ScoreBalance],[FrozenBalance],[FrozenScore],[UnionId]
            ,[ExpireTime],[CompanyName],[UserName],[IDCard],[Mobile],[PayPwd],[IsFocus],[FocusTime],[CancelTime],[IsTrade],[IsRealAuth]
            ,[IsGoldAuth],[IsCompanyAuth],[IsAuth],[RedOpenId],[PayOpenId],[AuthOpenId],[MsgOpenId],[LastLoginTime],[IsMobileAuth],
            ZXU.ZX_UserId,ZXU.ZX_UserName,ZXU.ZX_UserPwd,ZXU.ZX_UserLevel,ZXU.ZX_UserParentId,ZXU.ZX_UserSource,ZXU.ZX_Platform,ZXU.ZX_UserType,ZXU.JiFeng,
            ZXU.[BindPhone],ZXU.[BindQQ],ZXU.[BindWeiXin],ZXU.[BindZhiFuBao],ZXU.[BindBankCard],ZXU.[HaveEarnMoney]
              FROM [WeiXin_User] WU
              left join ZX_UserInfo ZXU ON 
              WU.UserID = ZXU.WX_UserId
 WHERE WU.UserCode = '{0}' and WU.[Password]='{1}' ", userCode,password);
            return repository.SqlQueryT<ZXUserInfoModel>(exeSql).FirstOrDefault();
        }
        public static WeiXinUser GetByUserCode(this IRepositoryAsync<WeiXinUser> repository, string userCode, string password)
        {
            Expression<Func<WeiXinUser, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(userCode))
            {
                express = express.AndAlso(i => i.UserCode == userCode);
            }
            if (!string.IsNullOrEmpty(password))
            {
                express = express.AndAlso(i => i.Password == password);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();

        }

        public static List<WeiXinUser> GetWXUserList(this IRepositoryAsync<WeiXinUser> repository, string site,   string nickName, string userCode, string mobile, int? state, DateTime? startTime, DateTime? endTime, string order, string ordername)
        {
            Expression<Func<WeiXinUser, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(nickName))
            {
                express = express.AndAlso(i =>i.NickName.Contains(nickName));
            }
            if (!string.IsNullOrEmpty(userCode))
            {
                express = express.AndAlso(i => i.UserCode.Contains(userCode));
            }
            if (!string.IsNullOrEmpty(mobile))
            {
                express = express.AndAlso(i => i.Mobile.Contains(mobile));
            }
            if (state.HasValue)
            {
                express = express.AndAlso(i => i.State==(state.Value));
            }
            if (startTime.HasValue)
            {
                express = express.AndAlso(i => i.CreateTime >= startTime.Value);
            }
            if (endTime.HasValue)
            {
                express = express.AndAlso(i => i.CreateTime < (endTime.Value));
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).ToList();
        }
    }
}
